package p.a5;

import gdp.stdlib.StdDraw;

public class Isogon {
	private final int	n;
	private Point[]		points;
	
	public Isogon(int n) {
		this.n = n;

		Point[] points = new Point[n];
		double angle_diff = 360. / n;
		double a = 0.0;
		double b = 0.0;
		for (int i = 0; i < n; i++) {
			a = 0.0 + (1.0 * Math.cos((i * Math.PI * angle_diff) / 180));
			b = 0.0 + (1.0 * Math.sin((i * Math.PI * angle_diff) / 180));
			points[i] = new Point(a, b);
		}
		this.points = points;
	}
	
	public Isogon() {
		this.n = 0;
		Point[] points = new Point[1];
		points[0] = new Point(0, 0);
	}
	
	@Override
	public String toString() {
		String result = "Isogon n=" + n;
		for (int i = 0; i < n; i++) {
			result = result + " " + points[i].toString();
		}
		return result;
	}

	public Isogon rotate(Angle beta) {
		Point[] np = new Point[this.n];
		// System.out.println(this.n + " " + n);
		for (int i = 0; i < this.n; i++) {
			Point px = new Point();
			px.setP(this.points[i].rotate(beta));
			np[i] = new Point(px);
		}
		Isogon result = new Isogon(this.n);
		result.setP(np);
		return result;
	}
	
	public Isogon translate(Point shift) {
		Point[] np = new Point[this.n];
		double a = 0.0;
		double b = 0.0;
		for (int i = 0; i < this.n; i++) {
			a = this.points[i].getX() + shift.getX();
			b = this.points[i].getY() + shift.getY();
			np[i] = new Point(a, b);
		}
		Isogon result = new Isogon(this.n);
		result.setP(np);
		return result;
	}
	
	public double[] getCX() {
		double[] result = new double[this.n + 1];
		for (int i = 0; i < this.n; i++) {
			result[i] = this.points[i].getX();
		}
		result[this.n] = this.points[0].getX();
		return result;
	}
	
	public double[] getCY() {
		double[] result = new double[this.n + 1];
		for (int i = 0; i < this.n; i++) {
			result[i] = this.points[i].getY();
		}
		result[this.n] = this.points[0].getY();
		return result;
	}
	
	public void draw() {
		StdDraw.polygon(this.getCX(), this.getCY());
	}
	
	public void setP(Point[] np) {
		for (int i = 0; i < n; i++) {
			this.points[i] = np[i];
		}
		
	}
	
}
